package cn.explink.dop.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.explink.dop.entity.vo.SystemParamVO;
import cn.explink.dop.service.SystemParamService;
import cn.explink.eap.core.common.model.json.AjaxJson;

@Controller
@RequestMapping("/systemParamController")
public class SystemParamController {

	@Autowired
	private SystemParamService systemParamService;

	@RequestMapping(params = "index")
	public ModelAndView storefrontIndex(HttpServletRequest request) {
		ModelAndView view = new ModelAndView("dop/system/systemParam");
		return view;
	}

	@RequestMapping(params = "getAllSystemParamList")
	@ResponseBody
	public Map<String, Object> getAllSystemParamList(int pageNum, int pageSize, HttpServletRequest request) {
		try {
			Map<String, Object> map = this.systemParamService.findDataByPage(pageNum, pageSize);
			// return this.systemParamService.getList(SystemParamVO.class);
			return map;
		} catch (Exception e) {
			return new HashMap<String, Object>();
		}
	}

	/**
	 *
	 * @Title: addParam
	 * @Description: 保存参数
	 * @author 孟凡岭
	 * @param @param id
	 * @param @param name
	 * @param @param value
	 * @param @param request
	 * @param @return
	 * @return AjaxJson 返回类型
	 * @throws
	 */
	@RequestMapping(params = "addParam")
	@ResponseBody
	public AjaxJson addParam(String id, String name, String value, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		SystemParamVO sys = new SystemParamVO();
		// sys.setId(id);
		sys.setName(name);
		sys.setValue(value);

		try {
			// this.systemParamService.addParam(sys);
			this.systemParamService.save(sys);
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return j;
	}

	/**
	 *
	 * @Title: updateParam
	 * @Description: 更新参数
	 * @author 孟凡岭
	 * @param @param id
	 * @param @param name
	 * @param @param value
	 * @param @param request
	 * @param @return
	 * @return AjaxJson 返回类型
	 * @throws
	 */
	@RequestMapping(params = "updateParam")
	@ResponseBody
	public AjaxJson updateParam(String id, String name, String value, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			this.systemParamService.updateParam(id, name, value);
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return j;
	}

	/**
	 *
	 * @Title: deleteParam
	 * @Description: 删除参数
	 * @author 孟凡岭
	 * @param @param id
	 * @param @param name
	 * @param @param value
	 * @param @param request
	 * @param @return
	 * @return AjaxJson 返回类型
	 * @throws
	 */
	@RequestMapping(params = "deleteParam")
	@ResponseBody
	public AjaxJson deleteParam(@RequestParam(value = "idList[]", required = false) List<String> idList, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			this.systemParamService.deleteParam(idList);
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return j;
	}
}
